package org.xiaozhiying.utils;

import org.xiaozhiying.entity.CityWeatherEntity;

import java.sql.*;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Date;

public class DataBaseUtil {
    private static Connection connection = null;

    static {
        String driverName = "com.mysql.cj.jdbc.Driver";
        // 东八区
        String url = "jdbc:mysql://127.0.0.1:3306/weather?serverTimezone=GMT%2B8";
        String user = "root";
        String pass = "1234567890";
        try {
            Class.forName(driverName);
            connection = DriverManager.getConnection(url, user, pass);
        } catch (ClassNotFoundException | SQLException e) {
            e.printStackTrace();
        }
    }


    public static void saveSingleCityWeatherInfo(CityWeatherEntity cityWeatherEntity) {
        if (null != connection) {
            try {
                PreparedStatement preparedStatement = connection.prepareStatement("insert into weather(province,city,adcode,weather,temperature,winddirection,windpower,humidity,reporttime) values(?,?,?,?,?,?,?,?,?)");
                preparedStatement.setString(1, cityWeatherEntity.getProvince());
                preparedStatement.setString(2, cityWeatherEntity.getCity());
                preparedStatement.setString(3, cityWeatherEntity.getAdcode());
                preparedStatement.setString(4, cityWeatherEntity.getWeather());
                preparedStatement.setString(5, cityWeatherEntity.getTemperature());
                preparedStatement.setString(6, cityWeatherEntity.getWinddirection());
                preparedStatement.setString(7, cityWeatherEntity.getWindpower());
                preparedStatement.setString(8, cityWeatherEntity.getHumidity());
                preparedStatement.setString(9, cityWeatherEntity.getReporttime());
                preparedStatement.execute();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            } finally {
                try {
                    connection.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static void saveAllCitysWeatherInfo(List<CityWeatherEntity> cityWeatherEntityList) {
        if (null != connection) {
            try {
                PreparedStatement preparedStatement = connection.prepareStatement("insert into weather(province,city,adcode,weather,temperature,winddirection,windpower,humidity,reporttime) values(?,?,?,?,?,?,?,?,?)");
                for (CityWeatherEntity cityWeatherEntity : cityWeatherEntityList) {
                    preparedStatement.setString(1, cityWeatherEntity.getProvince());
                    preparedStatement.setString(2, cityWeatherEntity.getCity());
                    preparedStatement.setString(3, cityWeatherEntity.getAdcode());
                    preparedStatement.setString(4, cityWeatherEntity.getWeather());
                    preparedStatement.setString(5, cityWeatherEntity.getTemperature());
                    preparedStatement.setString(6, cityWeatherEntity.getWinddirection());
                    preparedStatement.setString(7, cityWeatherEntity.getWindpower());
                    preparedStatement.setString(8, cityWeatherEntity.getHumidity());
                    preparedStatement.setString(9, cityWeatherEntity.getReporttime());
                    preparedStatement.addBatch();
                }
                preparedStatement.executeBatch();
                preparedStatement.close();
            } catch (SQLException e) {
                e.printStackTrace();
            } finally {
                try {
                    connection.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
        }
    }
}
